<script lang="ts">
export default {
  name: "BarDevelop"
};
</script>

<script setup lang="ts">
import { ECharts } from "echarts";
import echarts from "/@/plugins/echarts";
import { onMounted, nextTick, ref, watch } from "vue";
import { tryOnUnmounted, useTimeoutFn } from "@vueuse/core";
// import { useAppStoreHook } from "/@/store/modules/app";

import {
  addResizeListener,
  removeResizeListener
} from "element-plus/lib/utils/resize-event";

const props = defineProps({
  echartData: {
    type: Array,
    default: () => []
  }
});

// const pureApp = useAppStoreHook();

let echartInstance: ECharts;

const barDevelop = ref<HTMLElement | null>(null) as any;
const chart = ref<HTMLElement | null>(null) as any;

function initechartInstance(data1, data2, data3) {
  const echartDom = chart.value;
  if (!echartDom) return;
  // @ts-ignore
  echartInstance = echarts.init(echartDom);
  echartInstance.clear(); //清除旧画布 重新渲染

  echartInstance.setOption({
    color: ["#00e6ad", "#59a0ff", "#ffc500"],
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },

    legend: {
      // orient: "vertical",
      data: ["预算", "支出"]
    },
    grid: {
      bottom: "10%",
      height: "80%",
      containLabel: true
    },
    xAxis: [
      {
        type: "category",
        axisTick: {
          alignWithLabel: true
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          interval: 0,
          width: "70",
          overflow: "truncate"
        },
        data: data1
      }
    ],
    yAxis: [
      {
        axisLine: {
          show: true
        },
        splitLine: {
          show: false
        },
        type: "value"
      }
    ],
    series: [
      {
        name: "预算",
        type: "bar",
        label: {
          align: "center",
          show: true,
          verticalAlign: "bottom",
          position: "top"
        },
        data: data2
      },
      {
        name: "支出",
        type: "bar",
        label: {
          align: "center",
          show: true,
          verticalAlign: "bottom",
          position: "top"
        },
        data: data3
      }
    ]
  });
}

watch(
  () => props.echartData,
  newVal => {
    if (newVal.length) {
      nextTick(() => {
        const data1 = [];
        const data2 = [];
        const data3 = [];
        newVal.map((item, index) => {
          if (index > 0) {
            data1.push(item[0]);
            data2.push(item[1] || 0);
            data3.push(item[2] || 0);
          }
        });
        initechartInstance(data1, data2, data3);
      });
    }
  }
);

// onBeforeMount(() => {
//   nextTick(() => {
//     initechartInstance();
//   });
// });

onMounted(() => {
  nextTick(() => {
    addResizeListener(barDevelop.value, resetRender);
    // useEventListener("resize", () => {
    //   resetRender();
    // });
  });
});

// watch(
//   () => pureApp.sidebar.opened,
//   () => {
//     if(pureApp.sidebar.opened){
//      echartInstance.dispose();
//       echartInstance = null;
//       useTimeoutFn(() => {
//         nextTick(() => {
//           initechartInstance();
//         })
//       }, 300);
//     }
//   }
// );

const resetRender = () => {
  if (!echartInstance) return;
  useTimeoutFn(() => {
    echartInstance.resize();
  }, 180);
};

tryOnUnmounted(() => {
  if (!echartInstance) return;
  echartInstance.dispose();
  echartInstance = null;
  removeResizeListener(barDevelop.value, resetRender);
});
</script>

<template>
  <div ref="barDevelop">
    <div class="title">
      <slot> </slot>
    </div>
    <div class="barDevelop" ref="chart"></div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  font-size: 16px;
  color: $defaultColor;
  margin: 10px 0;
  height: 24px;
}
.barDevelop {
  width: 100%;
  height: 300px;
}
</style>
